<?php
//测试控制器类
class UserlistController extends Controller{

    private $db;

    public function __init(){
        //判断是否登陆
        $admin = Q('session.admin');
        if(empty($admin)){
            header('location:'.u('User/Login/Login'));
        }
        $this -> assign('admin',$admin);
        //指定函数
        $this -> db = k("User");
    }
    //动作方法
    public function index(){
        $op = Q('get.op','index');
        $type = Q('get.type','0');
        $uid = Q('get.uid','');
        $key = Q('get.key','');

        if($op == 'index'){
            if(empty($key)){
                //获取条件
                $where = 'type != "admin"';

                if($type != '0'){
                    $where .= ' and uvip = '.$type;
                }
            }else{
                $where = 'CONCAT(nickname,qq,uname,mobile,email) like "%'.$key.'%" and type != "admin"';
                $type = 0;
            }


            //获取用户数目
            $usernum = $this -> db -> usernum($where);
            //翻页参数
            $page = new page($usernum,10);
            $limit = $page->limit();

            $userlist = $this -> db -> userlist($where,$limit);
            //传递参数
            $this -> assign('type',$type);
            $this -> assign('page',$page->show(0));
            $this -> assign('userlist',$userlist);
            //显示视图
            $this->display('Template/admin/header');
            $this->display('left');
            $this->display();
        }elseif($op =='modify'){
            //判断是否传id
            if(empty($uid)){
                header('location:'.u('Userlist/index'));
                exit;
            }

            //查询该id的信息
            $where = 'uid = '.$uid;
            $userinfo = $this -> db -> userone($where);

            //传递参数
            $this -> assign('userinfo',$userinfo);
            //显示视图
            $this->display('Template/admin/header');
            $this->display('left');
            $this->display();
        }

    }

    //修改用户资料
    public function modify(){
        $uid = Q('get.uid','');

        $Model = K('user');

        if(empty($uid)){
            header('location:'.u('Userlist/index'));
            exit;
        }

        //表单验证
        if(!$Model -> create()){
            $this -> error($Model -> error);
            exit;
        }
        //将获取到的数据放入数组
        $data = array(
            'uname' => $_POST['username'],
            'nickname' => $_POST['nick'],
            'mobile' => $_POST['mobile'],
            'email' => $_POST['email'],
            'qq' => $_POST['qq'],
            'uvip' => $_POST['uvip'],
        );
        if(!empty($_POST['adpass'])){
            $pass = md5(md5($_POST['adpass']));
            $data['pw'] = $pass;
        }
        //条件
        $where = 'uid='.$uid;
        //修改用户密码
        if($this -> db -> upinfo($where,$data)){
            $user = $this -> db -> userone('uid ="'.$uid.'"');
            $this -> success($message = '修改成功。',U('Userlist/index'), $time = 2);
        }else{
            $this -> error('修改失败！');
            exit;
        }

    }

    //删除用户
    public function del(){
        $uid = Q('get.uid','');

        if(empty($uid)){
            header('location:'.u('Userlist/index'));
            exit;
        }
        $where = 'uid = '.$uid;
        if($this -> db -> userdel($where)){
            $this -> success($message = '删除成功。',U('Userlist/index'), $time = 2);
        }else{
            $this -> error('删除失败！');
            exit;
        }
    }

    //邀请码列表
    public function code(){

        if(isset($_POST['btn'])){
            $vip = Q('post.vip');
            //生成验证码
            $code = substr(md5(time()),0,6);
            $data = array(
                'code' => $code,
                'type' => $vip,
            );
            if($this -> db -> addcode($data)){
                //传递参数
                $this -> assign('vipcode',$code);
            }
        }

        //显示视图
        $this->display('Template/admin/header');
        $this->display('left');
        $this->display();
    }

}
